package com.zuoshen_algorithm;

import java.util.Stack;

/**
 * 项目名称: java_algorithm
 * @ClassName StackInverseOrder
 * @Description
 * @Author huipanxing huipanxing@chinasofti.com   @Date 2022/7/8 17:24
 */
public class StackInverseOrder {

    public static void main(String[] args) {
        Stack<Integer> stack=new Stack();
        stack.add(1);
        stack.add(2);
        stack.add(3);
//        stack.add(4);
//        stack.add(5);
        System.out.println(stack);
        reverse(stack);
        System.out.println(stack);
    }

    public static void reverse(Stack<Integer> stack){
        //如果栈为null 退出
        if (stack.isEmpty()){
            return;
        }
        //将栈底元素拿到
        int i = removeLastElement(stack);
        reverse(stack);
        //存储当前层的i值
        stack.push(i);
    }

    public static int removeLastElement(Stack<Integer> stack){
        int res = stack.pop();
        if (stack.isEmpty()){
            return res;
        }else {
            //返回的是递归函数的栈底的值
            int last = removeLastElement(stack);
            //存储当前层的res值
            stack.push(res);
            return last;
        }
    }

}
